package com.yuanxiao.filter;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

public class TPMap {
    public TPMap() {
    }

    private static ConcurrentHashMap<String, ArrayList<Double>> TP90Map =
            new ConcurrentHashMap<String, ArrayList<Double>>();

    private static ConcurrentHashMap<String, ArrayList<Double>> TP99Map =
            new ConcurrentHashMap<String, ArrayList<Double>>();


    public static ArrayList<Double> getTP90Map(String name){
        if(TP90Map.get(name)==null){
            TP90Map.put(name,new ArrayList<Double>());//第一次或已经一分钟清空
            //设定两个线程.打印及清空
            printTP(name);
            printMove(name);
        }
        return TP90Map.get(name);
    }

    public static ArrayList<Double> getTP99Map(String name){
        if(TP99Map.get(name)==null){
            TP99Map.put(name,new ArrayList<Double>());//第一次或已经一分钟清空
            //设定两个线程.打印及清空
            printTP(name);
            printMove(name);
        }
        return TP99Map.get(name);
    }

    private static void printTP(String name) {
        new Thread(()->{
            while (true){
                try {
                    TimeUnit.MILLISECONDS.sleep(5000);
                    ArrayList<Double> TP90Mapdoubles = TP90Map.get("methodA");
                    System.out.println("方法"+name+":的tp90耗时统计具体是"+TP90Mapdoubles);
                    ArrayList<Double> TP99Mapdoubles = TP99Map.get("methodA");
                    System.out.println("方法"+name+":的tp99耗时统计具体是"+TP99Mapdoubles);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }

    private static void printMove(String name) {
        new Thread(()->{
            while (true){
                try {
                    TimeUnit.MILLISECONDS.sleep(60000);
                    TP90Map.get(name).clear();
                    TP99Map.get(name).clear();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }
}
